package com.share.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @desc:
 * @author:caifan
 * @date:2022/1/26
 */
public class ProducerConsumer {

    private final Integer NOT_FULL = 0;
    private final Integer NOT_EMPTY = 10;
    private static Lock lock = new ReentrantLock();
    private static Condition conditionConsumer = lock.newCondition();
    private static Condition conditionProducer = lock.newCondition();

    public static void main(String[] args) {

    }
}

class Producer implements Runnable {
    private Lock lock;
    private Condition condition;
    private Integer notFull;

    public Producer(Lock lock, Condition condition, Integer notFull) {
        this.lock = lock;
        this.condition = condition;
        this.notFull = notFull;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                lock.lock();
                while (notFull >= 10) {
//                    condition.await();
                }

                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }

    }
}

class Consumer implements Runnable {
    private Lock lock;
    private Condition condition;
    private Integer notFull;

    public Consumer(Lock lock, Condition condition, Integer notFull) {
        this.lock = lock;
        this.condition = condition;
        this.notFull = notFull;
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                lock.lock();
                while (notFull == 0) {
//                    condition.await();
                }

                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }
}